// pages/clinicedit/clinicedit.js

var common = require('../../js/common.js');
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: ['上海市', '上海市', '浦东新区'],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '编辑诊所'
    })
    var id = options.id;

    if (id > 0) {
      this.setData({
        id: id
      })
      var that = this;
      //获取积分
      common.request(app.globalData.localUrl, 'yiyuanfind',
        { id: id },
        function (res) {
          console.log('获取编辑', res)
          if (res.data.code == 1) {
            // console.log(res.data.data.gsy_status)
            that.setData({
              binjidata: res.data.data,
              region: [res.data.data.address1, res.data.data.address2, res.data.data.address3]
            })
          }
        }
        , 'rtype= post');
    }


    console.log('options', options)
  },
  //省份选择
  bindRegionChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },

  formSubmit: function (e) {
    var that = this;

    wx.showLoading({
      title: '保存中',
      mask: true
    })
    console.log('name=' + e.detail.value.name)
    console.log('address=' + e.detail.value.address)
    console.log('phone=' + e.detail.value.phone)
    console.log('username=' + e.detail.value.username)
    console.log('user_id=' + wx.getStorageSync('userInfo').id)

    if (e.detail.value.name == '') {
      wx.showToast({
        title: '诊所名不为空',
        icon: 'none',
        duration: 1500
      })
      return;
    }

    if (e.detail.value.username == '') {
      wx.showToast({
        title: '联系人不为空',
        icon: 'none',
        duration: 1500
      })
      return;
    }
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (e.detail.value.phone.length === 0) {
      wx.showToast({
        title: '输入的手机号为空',
        icon: 'none',
        duration: 1500
      });
      return false;
    } else if (e.detail.value.phone.length < 11) {
      wx.showToast({
        title: '手机号长度有误！',
        icon: 'none',
        duration: 1500
      });
      return false;
    } else if (!myreg.test(e.detail.value.phone)) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'none',
        duration: 1500
      });
      return false;
    }
    if (e.detail.value.address == '') {
      wx.showToast({
        title: '地址不为空',
        icon: 'none',
        duration: 1500
      })
      return;
    }
    common.request(app.globalData.localUrl, 'edityiyuan', {
      name: e.detail.value.name,
      address: e.detail.value.address,
      phone: e.detail.value.phone,
      username: e.detail.value.username,
      user_id: wx.getStorageSync('userInfo').id,
      id:that.data.id,
      address1: that.data.region[0],
      address2: that.data.region[1],
      address3: that.data.region[2],

    },
      function (res) {
        console.log('编辑诊所', res)
        if (res.data.code == 1) {
          wx.showToast({
            title: '编辑成功',
            icon: 'success',
            mask: true
          })

          setTimeout(function () {
            wx.switchTab({
              url: '/pages/cliniclist/cliniclist'
            })

          }, 1500);

        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 1500
          })
        }
      }
    );

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})